<?php


namespace app\model\article;


use app\model\config\RegionModel;
use think\Model;
use think\model\concern\SoftDelete;

class ArticleModel extends Model
{
    use SoftDelete;
    protected $defaultSoftDelete = 0;

    protected $name = 'article';
    protected $autoWriteTimestamp = true;

    protected $json = ['city_ids'];
    protected $jsonAssoc = true;

    protected $hidden = ['delete_time', 'update_time'];

    protected $append = ['city', 'city_name'];

    public function setBeginTimeAttr($value)
    {
        return strtotime($value);
    }

    public function getBeginTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    public function setEndTimeAttr($value)
    {
        return strtotime($value);
    }

    public function getEndTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    /**
     * 获取城市名称
     * @param $value
     * @param $data
     * @return array
     */
    public function getCityAttr($value, $data)
    {
        if (!isset($data['city_ids'])) {
            return [];
        }
        $cityAttr = [];
        foreach ($data['city_ids'] as $k => $v) {
            $cityAttr[$k] = [
                'city_id' => $v,
                'name' => RegionModel::where('id', $v)->value('name')
            ];
        }
        return $cityAttr;
    }

    public function getCityNameAttr($value, $data)
    {
        if (!isset($data['city_ids'])) {
            return '';
        }
        $cityAttr = [];
        foreach ($data['city_ids'] as $k => $v) {
            $cityAttr[$k] = RegionModel::where('id', $v)->value('name');
        }
        return implode(',', $cityAttr);
    }

    public function getContentAttr($value, $data)
    {
        if (!$value) {
            return '';
        }
        return $value;
    }
}